﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.effects {
    import Engine.Classes.*;
    import Engine.Events.*;
    import flash.display.*;
    import flash.utils.*;
    import Classes.*;
    import Classes.util.*;
    import com.greensock.*;
    import flash.filters.*;

    public class StagePickEffect extends MapResourceEffect {

        public static const PICK_1:String = "coin1";
        public static const PICK_1_SMALL:String = "coin1small";
        public static const PICK_2:String = "coin2";
        public static const PICK_3:String = "coin3";
        public static const PICK_4:String = "moneybag1";
        public static const PICK_4_SMALL:String = "moneybag1small";
        public static const PICK_5:String = "moneybag2";
        public static const PICK_6:String = "moneybag3";
        public static const PICK_7:String = "gold1";
        public static const PICK_8:String = "gold2";
        public static const PICK_9:String = "gold3";
        public static const PICK_10:String = "diamond1";
        public static const PICK_11:String = "diamond2";
        public static const PICK_12:String = "diamond3";
        public static const PICK_BG:String = "guitarpic";
        public static const PICK_SHINE:String = "guitarpicanim";
        public static const PICK_SHINE_TALL:String = "guitarpicanim_tall";
        public static const MINI_STAR:String = "ministar";
        public static const PICK_GOODS:String = "goods";
        public static const PICK_TOURBUS:String = "tourBus";
        public static const PICK_LOCK:String = "lock";
        public static const PICK_POLICE:String = "police";
        public static const PICK_UPGRADE:String = "upgrade";
        public static const PICK_UPGRADE_HAMMER:String = "upgrade_hammer";
        public static const PICK_ATTENTION:String = "attention";
        public static const PICK_DONUT:String = "donut";
        public static const PICK_FORKLIFT:String = "forklift";
        public static const PICK_PREMIUM_GOODS:String = "premium_goods";
        public static const PICK_EXCLAMATION:String = "exclamation";
        public static const PICK_PEDDLERCENTER_DONUT:String = "peddlercenter_donut";
        public static const PICK_IN_TRANSIT:String = "in_transit";
        public static const PICK_TRAIN_AWAITING_CLEARANCE:String = "train_awaiting_clearance";
        public static const PICK_BINOCULARS:String = "binoculars";
        public static const PICK_HARDHAT:String = "hardhat";
        public static const PICK_OPENDOOR:String = "opendoor";
        public static const PICK_HOTEL:String = "hotel";
        public static const LOADED:String = "stage_pick_effect_loaded";
        public static const PICK_TIMER_GREEN:String = "timer_green";
        public static const PICK_TIMER_YELLOW:String = "timer_yellow";
        public static const PICK_TIMER_RED:String = "timer_red";
        private static const MAX_QUEUED_ACTIVE:int = 15;
        private static const QUEUED_ANIM_CYCLES:int = 3;

        private static var m_queuedFloatWait:Array = new Array();
        private static var m_queuedFloatActive:Array = new Array();

        protected const EFFECT_NAME:String = "stagePick";
        protected const PICK_FLOAT_DISTANCE:int = 5;
        protected const PICK_Y_OFFSET:int = 10;
        protected const PICK_Y_OFFSET_TALL:int = -10;
        protected const ICON_OFFSET:int = 10;
        protected const PICK_TWEEN_IN_TIME:Number = 0.75;
        protected const PICK_ANIMATE_DELAY:Number = 1000;
        protected const DISABLE_ANIMATION:Boolean = true;

        private var m_isComplete:Boolean = false;
        private var m_isLoading:Boolean = false;
        private var m_currentPickName:String = "";
        private var m_currentModifierName:String = "";
        private var m_isItemImagesLoaded:Boolean = false;
        private var m_itemImageInstances:Array;
        private var m_itemImageLoadedHash:Object;
        protected var m_currentPickImage:DisplayObject;
        protected var m_currentModifierImage:DisplayObject;
        protected var m_guitarPickImage:DisplayObject;
        protected var m_guitarPickAnimation:AnimatedBitmap;
        protected var m_ministarImage:DisplayObject;
        protected var m_showMinistar:Boolean = false;
        private var m_animTimeline:TimelineLite;
        protected var m_fadeTweenQueue:Array;
        protected var m_effectX:Number;
        protected var m_effectY:Number;
        protected var m_floatActive:Boolean = false;
        protected var m_queuedActive:Boolean = false;
        private var m_queuedCount:int = 0;
        private var m_startTime:int = -1;
        private var m_fadeTime:int = -1;
        private var m_glow:GlowFilter = null;
        private var m_glowColor:int = 0xFFFF;

        public function StagePickEffect(_arg1:MapResource){
            this.m_fadeTweenQueue = [];
            super(_arg1);
            this.m_glow = new GlowFilter(this.m_glowColor, 1, 1.5, 1.5, 30, 10);
            this.animate(1);
        }
        private static function activateQueuedPicks():void{
            var _local2:StagePickEffect;
            var _local3:Boolean;
            var _local4:StagePickEffect;
            var _local1:int;
            while (_local1 < m_queuedFloatActive.length) {
                _local2 = m_queuedFloatActive[_local1];
                _local3 = false;
                if (!_local2.m_queuedActive){
                    _local3 = true;
                };
                if (!_local2.m_animTimeline){
                    _local3 = true;
                };
                if (_local3){
                    m_queuedFloatActive.splice(_local1, 1);
                    _local1--;
                };
                _local1++;
            };
            while ((((m_queuedFloatWait.length > 0)) && ((m_queuedFloatActive.length < MAX_QUEUED_ACTIVE)))) {
                _local4 = m_queuedFloatWait[0];
                if (((_local4.m_guitarPickAnimation) && (_local4.m_currentPickImage))){
                    m_queuedFloatWait.splice(0, 1);
                    _local4.queuedActivate();
                } else {
                    m_queuedFloatWait.splice(0, 1);
                    m_queuedFloatWait.push(_local4);
                    break;
                };
            };
        }

        public function setFadeTime(_arg1:int):void{
            var seconds:* = _arg1;
            if ((((int(this.m_fadeTime) == -1)) && ((int(seconds) > 0)))){
                this.m_fadeTime = seconds;
                TimerUtil.callLater(function ():void{
                    if (m_animTimeline){
                        m_animTimeline.kill();
                    };
                    m_animTimeline = new TimelineLite();
                    m_animTimeline.appendMultiple([new TweenLite(this, 1, {alpha:0}), new TweenLite(this, 0.1, {onStart:fadeCallback})], 0, TweenAlign.SEQUENCE);
                }, (this.m_fadeTime * 1000));
            } else {
                if (((!(this.m_isItemImagesLoaded)) || (!(isMapResourceLoaded)))){
                    this.queueFade(seconds);
                };
            };
        }
        protected function queueFade(_arg1:int):void{
            this.m_fadeTweenQueue.push(_arg1);
        }
        protected function fadeCallback():void{
            m_mapResource.removeStagePickEffect();
            this.m_fadeTime = -1;
        }
        override public function animate(_arg1:uint):Boolean{
            if (this.m_isComplete){
                return (false);
            };
            var _local2:int = getTimer();
            if (this.m_startTime == -1){
                this.m_startTime = _local2;
            };
            if (_local2 < (this.m_startTime + this.PICK_ANIMATE_DELAY)){
                return (true);
            };
            if (((isMapResourceLoaded) && (!(this.m_isLoading)))){
                this.loadAnimationEffect();
            } else {
                if (this.m_guitarPickAnimation){
                    if (this.m_guitarPickAnimation.currentFrame >= (this.m_guitarPickAnimation.numFrames - 2)){
                        this.m_guitarPickAnimation.stop();
                        this.m_guitarPickAnimation.currentFrame = 0;
                    } else {
                        this.m_guitarPickAnimation.onUpdate((1 / 10));
                    };
                };
            };
            return (true);
        }
        override public function cleanUp():void{
            this.removeFromFloatQueues();
            if (this.m_currentPickImage){
                this.removeFromParent(this.m_currentPickImage);
                this.m_currentPickImage = null;
            };
            if (this.m_currentModifierImage){
                this.removeFromParent(this.m_currentModifierImage);
                this.m_currentModifierImage = null;
            };
            if (this.m_guitarPickAnimation){
                this.removeFromParent(this.m_guitarPickAnimation);
                this.m_guitarPickAnimation = null;
            };
            if (this.m_ministarImage){
                this.removeFromParent(this.m_ministarImage);
                this.m_ministarImage = null;
            };
            TweenLite.killTweensOf(this);
            this.m_itemImageLoadedHash = null;
            this.m_isComplete = true;
        }
        public function setPickType(_arg1:String, _arg2:String=null):void{
            if (!this.m_currentPickName){
                this.m_currentPickName = _arg1;
                this.m_currentModifierName = _arg2;
            } else {
                if (this.m_currentPickName != _arg1){
                    this.m_currentPickName = _arg1;
                    this.m_currentModifierName = _arg2;
                    this.reattach();
                };
            };
            if (!this.m_currentPickImage){
                this.m_currentPickName = _arg1;
                this.m_currentModifierName = _arg2;
                this.reattach();
            };
        }
        public function getPickType():String{
            return (this.m_currentPickName);
        }
        private function loadAnimationEffect():void{
            var _local3:XML;
            var _local4:ItemImage;
            var _local5:String;
            var _local1:XML = Global.gameSettings.getEffectByName(this.EFFECT_NAME);
            var _local2:XMLList = _local1.image;
            this.m_itemImageLoadedHash = {};
            for each (_local3 in _local2) {
                _local5 = _local3.attribute("name");
                this.m_itemImageLoadedHash[_local5] = null;
            };
            for each (_local3 in _local2) {
                _local4 = new ItemImage(_local3);
                _local4.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 0, true);
                _local4.load();
            };
            this.m_isLoading = true;
        }
        private function onItemImageLoaded(_arg1:LoaderEvent):void{
            var _local2:ItemImage;
            var _local3:ItemImageInstance;
            var _local4:String;
            var _local5:Boolean;
            if (((!(this.m_isItemImagesLoaded)) && (this.m_itemImageLoadedHash))){
                _local2 = (_arg1.target as ItemImage);
                _local2.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
                _local3 = _local2.getInstance();
                if (!_local3){
                    return;
                };
                _local4 = _local2.name;
                this.m_itemImageLoadedHash[_local4] = _local3;
                _local5 = true;
                for each (_local3 in this.m_itemImageLoadedHash) {
                    if (!_local3){
                        _local5 = false;
                        break;
                    };
                };
                if (_local5){
                    this.m_isItemImagesLoaded = true;
                    this.reattach();
                    dispatchEvent(new LoaderEvent(LoaderEvent.LOADED));
                };
            };
        }
        public function shine():void{
            if (!this.m_guitarPickAnimation){
                this.reattach();
            };
            if (this.m_guitarPickAnimation){
                this.m_guitarPickAnimation.play();
            };
        }
        public function float():void{
            var _local1:Number;
            if (this.DISABLE_ANIMATION){
                return;
            };
            if (this.m_queuedActive){
                this.stopFloat();
            };
            this.m_floatActive = true;
            if (((this.m_guitarPickAnimation) && (this.m_currentPickImage))){
                if (this.m_animTimeline){
                    this.m_animTimeline.gotoAndPlay(0);
                    return;
                };
                _local1 = (1.1 + (Math.random() / ((1 / (0.9 - 1.1)) + 1)));
                (this.m_currentPickImage as Bitmap).pixelSnapping = PixelSnapping.NEVER;
                this.m_guitarPickAnimation.pixelSnapping = PixelSnapping.NEVER;
                this.m_animTimeline = new TimelineLite({onComplete:this.floatCompleteHandler});
                this.m_animTimeline.appendMultiple([new TweenLite(this, _local1, {y:(this.y + this.PICK_FLOAT_DISTANCE)}), new TweenLite(this, _local1, {y:this.y})], 0, TweenAlign.SEQUENCE);
            };
        }
        public function queuedFloat():void{
            if (this.DISABLE_ANIMATION){
                return;
            };
            if (this.m_floatActive){
                this.stopFloat();
            };
            this.m_queuedActive = true;
            if ((((m_queuedFloatActive.indexOf(this) >= 0)) || ((m_queuedFloatWait.indexOf(this) >= 0)))){
                return;
            };
            m_queuedFloatWait.splice(0, 0, this);
            activateQueuedPicks();
        }
        private function queuedActivate():void{
            var _local1:Number = (1.1 + (Math.random() / ((1 / (0.9 - 1.1)) + 1)));
            var _local2:Number = (((1 - (m_queuedFloatActive.length / MAX_QUEUED_ACTIVE)) * 2) * _local1);
            (this.m_currentPickImage as Bitmap).pixelSnapping = PixelSnapping.NEVER;
            this.m_guitarPickAnimation.pixelSnapping = PixelSnapping.NEVER;
            this.cacheAsBitmap = (m_queuedFloatWait.length > 1);
            if (this.m_animTimeline == null){
                this.m_animTimeline = new TimelineLite({onComplete:this.queuedFloatCompleteHandler});
                this.m_animTimeline.appendMultiple([new TweenLite(this, _local1, {y:(this.y + this.PICK_FLOAT_DISTANCE)}), new TweenLite(this, _local1, {y:this.y})], 0, TweenAlign.SEQUENCE);
            } else {
                this.m_animTimeline.gotoAndPlay(0);
            };
            this.m_queuedCount = QUEUED_ANIM_CYCLES;
            m_queuedFloatActive.push(this);
        }
        private function removeFromFloatQueues():void{
            var _local1:int = m_queuedFloatActive.indexOf(this);
            if (_local1 >= 0){
                m_queuedFloatActive.splice(_local1, 1);
            };
            _local1 = m_queuedFloatWait.indexOf(this);
            if (_local1 >= 0){
                m_queuedFloatWait.splice(_local1, 1);
            };
        }
        private function queuedFloatCompleteHandler():void{
            if (((((this.m_queuedActive) && (this.m_animTimeline))) && ((this.m_queuedCount > 1)))){
                this.m_queuedCount--;
                this.m_animTimeline.gotoAndPlay(0);
            } else {
                this.removeFromFloatQueues();
                if (((!((this.m_animTimeline == null))) && (this.m_queuedActive))){
                    m_queuedFloatWait.push(this);
                };
            };
            activateQueuedPicks();
        }
        public function stopFloat():void{
            this.removeFromFloatQueues();
            this.m_floatActive = false;
            this.m_queuedActive = false;
            if (this.m_animTimeline){
                this.m_animTimeline.kill();
            };
            this.m_animTimeline = null;
        }
        private function floatCompleteHandler():void{
            this.float();
        }
        public function showBonusXp():void{
            if (!this.m_currentPickImage){
                this.reattach();
            } else {
                if (this.m_ministarImage){
                    this.m_ministarImage.visible = true;
                    this.m_showMinistar = true;
                };
            };
        }
        public function hideBonusXp():void{
            this.m_showMinistar = false;
            if (this.m_ministarImage){
                this.m_ministarImage.visible = false;
            };
        }
        override public function reattach():void{
            if (((!(this.m_isItemImagesLoaded)) || (!(isMapResourceLoaded)))){
                return;
            };
            this.alpha = 0;
            var _local1:Sprite = this.getAttachParent();
            if (((!(this.parent)) || (((this.parent) && (!((this.parent == _local1))))))){
                _local1.addChild(this);
            };
            this.setPickImage(this.m_currentPickName, this.m_currentModifierName);
            TweenLite.to(this, this.PICK_TWEEN_IN_TIME, {alpha:1});
            if (this.m_fadeTweenQueue){
                this.setFadeTime(this.m_fadeTweenQueue.shift());
            };
        }
        protected function removeFromParent(_arg1:DisplayObject):void{
            if (((_arg1) && (_arg1.parent))){
                _arg1.parent.removeChild(_arg1);
            };
        }
        protected function getImage(_arg1:String):Object{
            var _local2:ItemImageInstance = this.m_itemImageLoadedHash[_arg1];
            var _local3:Object = _local2.image;
            return (_local3);
        }
        private function getAttachParent():Sprite{
            var _local1:Sprite = (m_mapResource.getDisplayObject() as Sprite);
            return (_local1);
        }
        protected function setPickImage(_arg1:String, _arg2:String):void{
            var _local4:String;
            var _local5:IsoRect;
            var _local6:Number;
            this.removeFromParent(this.m_currentPickImage);
            this.removeFromParent(this.m_currentModifierImage);
            this.removeFromParent(this.m_guitarPickAnimation);
            this.removeFromParent(this.m_ministarImage);
            var _local3 = !((_arg2 == null));
            if (_arg1){
                _local4 = (_local3) ? PICK_SHINE_TALL : PICK_SHINE;
                this.m_guitarPickAnimation = (this.getImage(_local4) as AnimatedBitmap);
                this.m_guitarPickAnimation.x = ((-(this.m_guitarPickAnimation.width) >> 1) + 1);
                if (_arg1 == PICK_4){
                    this.m_guitarPickAnimation.x = ((-(this.m_guitarPickAnimation.width) >> 1) + 1.5);
                };
                this.m_guitarPickAnimation.y = ((-(this.m_guitarPickAnimation.height) >> 1) + (_local3) ? -12 : 3);
                this.m_currentPickImage = (this.getImage(_arg1) as DisplayObject);
                this.m_currentPickImage.x = (-(this.m_currentPickImage.width) * 0.5);
                this.m_currentPickImage.y = (-(this.m_currentPickImage.height) * 0.5);
                this.m_currentModifierImage = ((_arg2)!=null) ? (this.getImage(_arg2) as DisplayObject) : new Sprite();
                this.m_currentModifierImage.x = (-(this.m_currentModifierImage.width) * 0.5);
                this.m_currentModifierImage.y = (-(this.m_currentModifierImage.height) * 1.5);
                this.m_ministarImage = (this.getImage(MINI_STAR) as DisplayObject);
                this.m_ministarImage.x = (this.m_currentPickImage.x + (this.m_currentPickImage.width * 0.6));
                this.m_ministarImage.y = (this.m_currentPickImage.y + (this.m_currentPickImage.height * 0.1));
                if (!this.m_showMinistar){
                    this.m_ministarImage.visible = false;
                };
                _local5 = IsoRect.getIsoRectFromSize(m_mapResource.getReference().getSize());
                this.m_effectX = _local5.bottom.x;
                this.m_effectY = (_local5.top.y + (this.m_guitarPickAnimation.height / 2));
                this.m_guitarPickAnimation.stop();
                this.addChild(this.m_guitarPickAnimation);
                this.addChild(this.m_currentPickImage);
                this.addChild(this.m_currentModifierImage);
                this.addChild(this.m_ministarImage);
                _local6 = (_local3) ? this.PICK_Y_OFFSET_TALL : this.PICK_Y_OFFSET;
                this.y = ((this.m_effectY + _local6) - this.PICK_FLOAT_DISTANCE);
                this.x = this.m_effectX;
            };
            if (this.m_floatActive){
                this.float();
            } else {
                if (this.m_queuedActive){
                    this.queuedFloat();
                };
            };
        }
        public function getAnimationHeight():int{
            var _local1:IsoRect;
            if (m_mapResource){
                _local1 = IsoRect.getIsoRectFromSize(m_mapResource.getReference().getSize());
                return (-((_local1.top.y + this.PICK_FLOAT_DISTANCE)));
            };
            return (0);
        }
        public function showHighlight():void{
            this.filters = [this.m_glow];
        }
        public function hideHighlight():void{
            this.filters = [];
        }
        protected function get glow():int{
            return (this.m_glowColor);
        }
        protected function set glow(_arg1:int):void{
            this.m_glowColor = _arg1;
            this.m_glow = new GlowFilter(_arg1, 1, 1.5, 1.5, 100, 100);
        }

    }
}//package Classes.effects 
